<?php
/**
 * Created by PhpStorm.
 * User: HS032
 * Date: 2020/9/3
 * Time: 14:55
 */

namespace ticket\modules\backend\controllers;


use ticket\libs\Tools;
use ticket\modules\backend\models\TicketCategorys;
use Yii;


/*** 门票分类
 * Class TicketCategoryController
 * @package ticket\modules\backend\controllers
 */
class TicketCategoryController extends BasicController
{
    public function actionCategoryList()
    {
        $app_id = $this->appId;
        if (empty($app_id)){
            return Tools::showRes(10404,'参数有误');
        }
        $categoryList = TicketCategorys::getTreeList(0,$app_id);
        return Tools::showRes(0,'success',[
            'categoryList' => $categoryList,
        ]);

        
    }

    /*** 添加分类
     * @return false|string
     */
    public function actionAddCategory()
    {
        $post = Yii::$app->request->post();
        $app_id = $this->appId;
        $categoryModel = new TicketCategorys();
        if ($categoryModel->addCategory($app_id,$post)){
            return Tools::showRes(0,'添加成功');
        }else{
            if ($categoryModel->hasErrors()){
                return Tools::showRes(10044,$categoryModel->getErrors());
            }else{
                return Tools::showRes(10045,'添加失败');
            }
        }

    }

    /*** 修改分类
     * @return false|string
     * @throws \yii\base\ExitException
     */
    public function actionModCategory()
    {
        if (Yii::$app->request->isPost){
            $post = Yii::$app->request->post();
            $id = (int)(isset($post['cat_id'])?$post['cat_id']:0);
            if (!$id){
                return Tools::showRes(300,'参数有误');
                Yii::$app->end();
            }
            $articleCategoryModel = new TicketCategorys();
            if($articleCategoryModel->modCategory($id, $post)){
                return Tools::showRes(0, '修改成功');
            }else{
                if($articleCategoryModel->hasErrors()){
                    return Tools::showRes(10044, $articleCategoryModel->getErrors());
                }else{
                    return Tools::showRes(10045,'修改失败');
                }
            }
        }

        $get = Yii::$app->request->get();
        $id = (int)(isset($get['id'])?$get['id']:0);
        if (!$id){
            return Tools::showRes(10404,'参数有误');
            Yii::$app->end();
        }
        $app_id = $this->appId;
        if (empty($app_id)){
            return Tools::showRes(10404,'参数有误');
        }

        $categoryList = TicketCategorys::getTreeList(0,$app_id);
        $ticketCategory = TicketCategorys::find()->where('cat_id = :cid',[':cid' => $id])->asArray()->one();

        return Tools::showRes(0,'success',[
            'categoryList' => $categoryList,
            'ticketCategory' => $ticketCategory,
        ]);

    }

    /*** 删除分类
     * @return false|string
     * @throws \yii\base\ExitException
     */
    public function actionDelCategory()
    {
        if (Yii::$app->request->isPost){
            $post = Yii::$app->request->post();
            $id = (int)(isset($post['id'])?$post['id']:0);
            if (!$id){
                return Tools::showRes(10404,'参数有误');
                Yii::$app->end();
            }
            $app_id = $this->appId;
            $articleCategory = new TicketCategorys();
            if ($articleCategory->delCategory($id,$app_id)){
                return Tools::showRes(0, '删除成功');
                Yii::$app->end();
            }else{
                if($articleCategory->hasErrors()){
                    return Tools::showRes(10044, $articleCategory->getErrors());
                }else{
                    return Tools::showRes(10045);
                }
            }
        }
    }


}